package com.example.langchain4j.config;

import dev.langchain4j.model.chat.ChatLanguageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 统一的 ChatLanguageModel 配置
 * 根据配置文件自动选择使用哪个模型
 */
@Configuration
public class UnifiedChatModelConfig {

    /**
     * 本地模式：使用 OpenAI (MindIE)
     */
    @Bean(name = "chatLanguageModel")
    @ConditionalOnProperty(name = "langchain4j.open-ai.chat-model.base-url")
    public ChatLanguageModel chatLanguageModelLocal(
            @Autowired(required = false) @Qualifier("openAiChatModel") ChatLanguageModel openAiChatModel) {
        if (openAiChatModel == null) {
            throw new IllegalStateException("OpenAI ChatModel not configured for local mode");
        }
        return openAiChatModel;
    }

    /**
     * 云端模式：使用 DashScope (千问)
     */
    @Bean(name = "chatLanguageModel")
    @ConditionalOnProperty(name = "langchain4j.open-ai.chat-model.base-url", matchIfMissing = true, havingValue = "false")
    public ChatLanguageModel chatLanguageModelCloud(
            @Autowired(required = false) @Qualifier("qwenChatModel") ChatLanguageModel qwenChatModel) {
        if (qwenChatModel == null) {
            throw new IllegalStateException("DashScope ChatModel not configured for cloud mode");
        }
        return qwenChatModel;
    }
}